package com.link.base.base.orderpayset.controller;

import com.link.base.base.orderpayset.model.OrderPaySet;
import com.link.base.base.orderpayset.service.OrderPaySetService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liushoulong
 * @date 2018/4/27
 * @comments 参数设置 订单类型参数 订单支付设置
 */
@Controller
@RequestMapping("/link/orderPaySet")
public class OrderPaySetController extends BasicController<OrderPaySet> {

    /***
     * 更新订单类型设置
     * @param orderParamsModel
     * @return
     */
//    @Deprecated
//    @RequestMapping("/updateOrdParamsModel")
//    @ResponseBody
//    public Map<String,Object> updateOrdParamsModel(@RequestBody  OrderParamsModel orderParamsModel){
//        Map<String,Object>  result = new HashMap<String,Object>();
//        try{
//            result = orderPaySetService.batchUpdateOrderParamsModel(orderParamsModel);
//        }catch(Exception e){
//            result.put("success", false);
//            result.put("newRow", new ArrayList<OrderPaySet>());
//            result.put("result", e.getMessage());
//        }
//        return result;
//    }

    @Resource
    private OrderPaySetService orderPaySetService;

    /**
     * 新建或者更新
     *
     * @param orderPaySet
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryByKeys")
    @ResponseBody
    public Map<String, Object> queryByKeys(OrderPaySet orderPaySet, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        /*
         * 1 根据一级 二级 查询
         *   如果不存在   创建一条新纪录
         *   如果存在   返回该数据
         */

        try {
            beforInsert(orderPaySet, null);
            OrderPaySet orderPaySetRet = orderPaySetService.getByOrderType(orderPaySet);
            result.put("success", true);
            result.put("newRow", orderPaySetRet);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @Override
    public void beforInsert(OrderPaySet orderPaySet, HttpServletRequest request) throws Exception {
        if (StringUtils.isEmpty(orderPaySet.getOrderKind())
                || StringUtils.isEmpty(orderPaySet.getOrderFirstType()) || StringUtils.isEmpty(orderPaySet.getOrderSecondType())
                || StringUtils.isEmpty(orderPaySet.getOrderSource())) {
            throw new ServiceException("PUBLIC-024");
        }
    }

    @Override
    public void beforUpdate(OrderPaySet orderPaySet, HttpServletRequest request) throws Exception {
        if (StringUtils.isEmpty(orderPaySet.getOrderKind())
                || StringUtils.isEmpty(orderPaySet.getOrderFirstType()) || StringUtils.isEmpty(orderPaySet.getOrderSecondType())
                || StringUtils.isEmpty(orderPaySet.getOrderSource())) {
            throw new ServiceException("PUBLIC-024");
        }
    }

    /**
     * 因业务调整，暂时预留
     * 获取订单类型设置数据
     * @param orderPaySet
     * @param
     * @return
     */
/*    @Deprecated
    @RequestMapping("/getOrderTypeSet")
    @ResponseBody
    public Map<String,Object> getOrderTypeSet(OrderPaySet orderPaySet, HttpSession session){
        Map<String,Object> result = new HashMap<String,Object>();
        try{
            result = orderPaySetService.getOrderPaySet(orderPaySet,session);
        }catch (Exception e){
            LCLogger.withException(e);
            result.put("success", false);
            result.put("newRow", new ArrayList<OrderParamsModel>());
            result.put("result", e.getMessage());
        }
        return result;
    }*/

    @Override
    public void beforUpsert(OrderPaySet orderPaySet, HttpServletRequest request) throws Exception {
        if (StringUtils.isEmpty(orderPaySet.getOrderKind())) {
            throw new BasicControllerException("请选择订单大类");
        }
        if (StringUtils.isEmpty(orderPaySet.getOrderFirstType())) {
            throw new BasicControllerException("请选择订单类型");
        }
        if (StringUtils.isEmpty(orderPaySet.getOrderSecondType())) {
            throw new BasicControllerException("请选择订单子类型");
        }
        if (StringUtils.isEmpty(orderPaySet.getOrderSource())) {
            throw new BasicControllerException("请选择订单来源");
        }
        if (StringUtils.isEmpty(orderPaySet.getRow_status())) {
            throw new BasicControllerException("操作状态异常");
        }
        OrderPaySet params = new OrderPaySet();
        if (orderPaySet.getLoginCorpId() == null) {
            params.setLoginCorpId(UserUtil.getUser().getLoginCorpId());
        } else {
            params.setLoginCorpId(orderPaySet.getLoginCorpId());
        }
        params.setSqlAndCorpId("Y");
        params.setOrderKind(orderPaySet.getOrderKind());
        params.setOrderFirstType(orderPaySet.getOrderFirstType());
        params.setOrderSecondType(orderPaySet.getOrderSecondType());
        params.setOrderSource(orderPaySet.getOrderSource());
        params.setSyncFlag(orderPaySet.getSyncFlag());
        List<OrderPaySet> list = orderPaySetService.queryAll(params);
        String newStatus = "NEW";
        String updateStatus = "UPDATE";
        if (!list.isEmpty()) {
            if (list.size() == 1) {
                if (StringUtils.equals(newStatus, orderPaySet.getRow_status())) {
                    throw new BasicControllerException("创建参数类型已存在");
                }
                // 先注释掉，避免点编辑按钮，不执行跟新操作，会报错
                /* else if (StringUtils.equals(updateStatus, orderPaySet.getRow_status())) {
                    OrderPaySet ops = list.get(0);
                    if (ops.getId() != orderPaySet.getId()) {
                        throw new BasicControllerException("更新参数类型已存在");
                    }
                }*/
            } else {
                throw new BasicControllerException("操作参数类型存在多条");
            }
        }
    }

    @Override
    public BasicService<OrderPaySet> getBasicService() throws Exception {
        return orderPaySetService;
    }

}
